home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 October / Chip Ekim 2003.iso / prog / tamsurum / tocom / Security Updates / SQL-MSDE-CriticalUpdate_ENU.msi / Hotfix3 / Files / qfe356326.sql < prev    next >
Encoding:
Text File  |  2002-01-02  |  17.8 KB  |  390 lines

  1. --------------------------------------------------------------------------------
  2. -- delete proxy credentials from LSA when @sysadmin_only is turned on
  3. --------------------------------------------------------------------------------
  4.  
  5. use msdb
  6. GO
  7. SET QUOTED_IDENTIFIER ON 
  8. GO
  9. SET ANSI_NULLS ON 
  10. GO
  11.  
  12.  
  13. ALTER PROCEDURE dbo.sp_set_sqlagent_properties
  14.   @auto_start                  INT           = NULL, -- 1 or 0
  15.   -- Non-SQLDMO exposed properties
  16.   @sqlserver_restart           INT           = NULL, -- 1 or 0
  17.   @jobhistory_max_rows         INT           = NULL, -- No maximum = -1, otherwise must be > 1
  18.   @jobhistory_max_rows_per_job INT           = NULL, -- 1 to @jobhistory_max_rows
  19.   @errorlog_file               NVARCHAR(255) = NULL, -- Full drive\path\name of errorlog file
  20.   @errorlogging_level          INT           = NULL, -- 1 = error, 2 = warning, 4 = information
  21.   @error_recipient             NVARCHAR(30)  = NULL, -- Network address of error popup recipient
  22.   @monitor_autostart           INT           = NULL, -- 1 or 0
  23.   @local_host_server           NVARCHAR(30)  = NULL, -- Alias of local host server
  24.   @job_shutdown_timeout        INT           = NULL, -- 5 to 600 seconds
  25.   @cmdexec_account             VARBINARY(64) = NULL, -- CmdExec account information
  26.   @regular_connections         INT           = NULL, -- 1 or 0
  27.   @host_login_name             sysname       = NULL, -- Login name (if regular_connections = 1)
  28.   @host_login_password         VARBINARY(512) = NULL, -- Login password (if regular_connections = 1)
  29.   @login_timeout               INT           = NULL, -- 5 to 45 (seconds)
  30.   @idle_cpu_percent            INT           = NULL, -- 1 to 100
  31.   @idle_cpu_duration           INT           = NULL, -- 20 to 86400 seconds
  32.   @oem_errorlog                INT           = NULL, -- 1 or 0
  33.   @sysadmin_only               INT           = NULL, -- 1 or 0
  34.   @email_profile               NVARCHAR(64)  = NULL, -- Email profile name
  35.   @email_save_in_sent_folder   INT           = NULL, -- 1 or 0
  36.   @cpu_poller_enabled          INT           = NULL  -- 1 or 0
  37. AS
  38. BEGIN
  39.   -- NOTE: We set all SQLServerAgent properties at one go for performance reasons.
  40.   -- NOTE: You cannot set the value of the properties msx_server_name, is_msx or
  41.   --       startup_account - they are all read only.
  42.  
  43.   DECLARE @res_valid_range           NVARCHAR(100)
  44.   DECLARE @existing_core_engine_mask INT
  45.  
  46.   SET NOCOUNT ON
  47.  
  48.   -- Remove any leading/trailing spaces from parameters
  49.   SELECT @errorlog_file     = LTRIM(RTRIM(@errorlog_file))
  50.   SELECT @error_recipient   = LTRIM(RTRIM(@error_recipient))
  51.   SELECT @local_host_server = LTRIM(RTRIM(@local_host_server))
  52.   SELECT @host_login_name   = LTRIM(RTRIM(@host_login_name))
  53.   SELECT @email_profile     = LTRIM(RTRIM(@email_profile))
  54.  
  55.   -- Make sure values (if supplied) are good
  56.   IF (@auto_start IS NOT NULL)
  57.   BEGIN
  58.     -- NOTE: When setting the the services start value, 2 == auto-start, 3 == Don't auto-start
  59.     SELECT @auto_start = CASE @auto_start
  60.                            WHEN 0 THEN 3
  61.                            WHEN 1 THEN 2
  62.                            ELSE 3 -- Assume non auto-start if passed a junk value
  63.                           END
  64.   END
  65.  
  66.   -- Non-SQLDMO exposed properties
  67.   IF ((@sqlserver_restart IS NOT NULL) AND (@sqlserver_restart <> 0))
  68.     SELECT @sqlserver_restart = 1
  69.  
  70.   IF (@jobhistory_max_rows IS NOT NULL)
  71.   BEGIN
  72.     SELECT @res_valid_range = FORMATMESSAGE(14207)
  73.     IF ((@jobhistory_max_rows < -1) OR (@jobhistory_max_rows = 0))
  74.     BEGIN
  75.       RAISERROR(14266, -1, -1, '@jobhistory_max_rows', @res_valid_range)
  76.       RETURN(1) -- Failure
  77.     END
  78.   END
  79.   ELSE
  80.   BEGIN
  81.     EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  82.                                            N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  83.                                            N'JobHistoryMaxRows',
  84.                                            @jobhistory_max_rows OUTPUT,
  85.                                            N'no_output'
  86.     SELECT @jobhistory_max_rows = ISNULL(@jobhistory_max_rows, -1)
  87.   END
  88.  
  89.   IF (@jobhistory_max_rows_per_job IS NOT NULL)
  90.   BEGIN
  91.     IF (@jobhistory_max_rows = -1)
  92.       SELECT @jobhistory_max_rows_per_job = 0
  93.     ELSE
  94.     BEGIN
  95.       IF ((@jobhistory_max_rows_per_job < 1) OR (@jobhistory_max_rows_per_job > @jobhistory_max_rows))
  96.       BEGIN
  97.         SELECT @res_valid_range = N'1..' + CONVERT(NVARCHAR, @jobhistory_max_rows)
  98.         RAISERROR(14266, -1, -1, '@jobhistory_max_rows', @res_valid_range)
  99.         RETURN(1) -- Failure
  100.       END
  101.     END
  102.   END
  103.  
  104.   IF (@errorlogging_level IS NOT NULL) AND ((@errorlogging_level < 1) OR (@errorlogging_level > 7))
  105.   BEGIN
  106.     RAISERROR(14266, -1, -1, '@errorlogging_level', '1..7')
  107.     RETURN(1) -- Failure
  108.   END
  109.  
  110.   IF (@monitor_autostart IS NOT NULL) AND ((@monitor_autostart < 0) OR (@monitor_autostart > 1))
  111.   BEGIN
  112.     RAISERROR(14266, -1, -1, '@monitor_autostart', '0, 1')
  113.     RETURN(1) -- Failure
  114.   END
  115.  
  116.   IF (@job_shutdown_timeout IS NOT NULL) AND ((@job_shutdown_timeout < 5) OR (@job_shutdown_timeout > 600))
  117.   BEGIN
  118.     RAISERROR(14266, -1, -1, '@job_shutdown_timeout', '5..600')
  119.     RETURN(1) -- Failure
  120.   END
  121.  
  122.   IF (@regular_connections IS NOT NULL) AND ((@regular_connections < 0) OR (@regular_connections > 1))
  123.   BEGIN
  124.     RAISERROR(14266, -1, -1, '@regular_connections', '0, 1')
  125.     RETURN(1) -- Failure
  126.   END
  127.  
  128.   IF (@login_timeout IS NOT NULL) AND ((@login_timeout < 5) OR (@login_timeout > 45))
  129.   BEGIN
  130.     RAISERROR(14266, -1, -1, '@login_timeout', '5..45')
  131.     RETURN(1) -- Failure
  132.   END
  133.  
  134.   IF ((@idle_cpu_percent IS NOT NULL) AND ((@idle_cpu_percent < 1) OR (@idle_cpu_percent > 100)))
  135.   BEGIN
  136.     RAISERROR(14266, -1, -1, '@idle_cpu_percent', '10..100')
  137.     RETURN(1) -- Failure
  138.   END
  139.  
  140.   IF ((@idle_cpu_duration IS NOT NULL) AND ((@idle_cpu_duration < 20) OR (@idle_cpu_duration > 86400)))
  141.   BEGIN
  142.     RAISERROR(14266, -1, -1, '@idle_cpu_duration', '20..86400')
  143.     RETURN(1) -- Failure
  144.   END
  145.  
  146.   IF (@oem_errorlog IS NOT NULL) AND ((@oem_errorlog < 0) OR (@oem_errorlog > 1))
  147.   BEGIN
  148.     RAISERROR(14266, -1, -1, '@oem_errorlog', '0, 1')
  149.     RETURN(1) -- Failure
  150.   END
  151.  
  152.   IF (@sysadmin_only IS NOT NULL) AND ((@sysadmin_only < 0) OR (@sysadmin_only > 1))
  153.   BEGIN
  154.     RAISERROR(14266, -1, -1, '@sysadmin_only', '0, 1')
  155.     RETURN(1) -- Failure
  156.   END
  157.  
  158.   IF (@email_save_in_sent_folder IS NOT NULL) AND ((@email_save_in_sent_folder < 0) OR (@email_save_in_sent_folder > 1))
  159.   BEGIN
  160.     RAISERROR(14266, -1, -1, 'email_save_in_sent_folder', '0, 1')
  161.     RETURN(1) -- Failure
  162.   END
  163.  
  164.   IF (@cpu_poller_enabled IS NOT NULL) AND ((@cpu_poller_enabled < 0) OR (@cpu_poller_enabled > 1))
  165.   BEGIN
  166.     RAISERROR(14266, -1, -1, 'cpu_poller_enabled', '0, 1')
  167.     RETURN(1) -- Failure
  168.   END
  169.  
  170.   -- Write out the values
  171.   IF (@auto_start IS NOT NULL)
  172.   BEGIN
  173.     IF ((PLATFORM() & 0x1) = 0x1) -- NT
  174.     BEGIN
  175.       DECLARE @key NVARCHAR(200)
  176.  
  177.       SELECT @key = N'SYSTEM\CurrentControlSet\Services\'
  178.       IF (SERVERPROPERTY('INSTANCENAME') IS NOT NULL)
  179.         SELECT @key = @key + N'SQLAgent$' + CONVERT (sysname, SERVERPROPERTY('INSTANCENAME'))
  180.       ELSE
  181.         SELECT @key = @key + N'SQLServerAgent'
  182.  
  183.       EXECUTE master.dbo.xp_regwrite N'HKEY_LOCAL_MACHINE',
  184.                                      @key,
  185.                                      N'Start',
  186.                                      N'REG_DWORD',
  187.                                      @auto_start
  188.     END
  189.     ELSE
  190.       RAISERROR(14546, 16, 1, '@auto_start')
  191.   END
  192.  
  193.   -- Non-SQLDMO exposed properties
  194.   IF (@sqlserver_restart IS NOT NULL)
  195.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  196.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  197.                                             N'RestartSQLServer',
  198.                                             N'REG_DWORD',
  199.                                             @sqlserver_restart
  200.   IF (@jobhistory_max_rows IS NOT NULL)
  201.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  202.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  203.                                             N'JobHistoryMaxRows',
  204.                                             N'REG_DWORD',
  205.                                             @jobhistory_max_rows
  206.   IF (@jobhistory_max_rows_per_job IS NOT NULL)
  207.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  208.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  209.                                             N'JobHistoryMaxRowsPerJob',
  210.                                             N'REG_DWORD',
  211.                                             @jobhistory_max_rows_per_job
  212.   IF (@errorlog_file IS NOT NULL)
  213.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  214.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  215.                                             N'ErrorLogFile',
  216.                                             N'REG_SZ',
  217.                                             @errorlog_file
  218.   IF (@errorlogging_level IS NOT NULL)
  219.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  220.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  221.                                             N'ErrorLoggingLevel',
  222.                                             N'REG_DWORD',
  223.                                             @errorlogging_level
  224.   IF (@error_recipient IS NOT NULL)
  225.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  226.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  227.                                             N'ErrorMonitor',
  228.                                             N'REG_SZ',
  229.                                             @error_recipient
  230.   IF (@monitor_autostart IS NOT NULL)
  231.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  232.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  233.                                             N'MonitorAutoStart',
  234.                                             N'REG_DWORD',
  235.                                             @monitor_autostart
  236.   IF (@local_host_server IS NOT NULL)
  237.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  238.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  239.                                             N'ServerHost',
  240.                                             N'REG_SZ',
  241.                                             @local_host_server
  242.   IF (@job_shutdown_timeout IS NOT NULL)
  243.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  244.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  245.                                             N'JobShutdownTimeout',
  246.                                             N'REG_DWORD',
  247.                                             @job_shutdown_timeout
  248.   IF (@cmdexec_account IS NOT NULL)
  249.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  250.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  251.                                             N'CmdExecAccount',
  252.                                             N'REG_BINARY',
  253.                                             @cmdexec_account
  254.   IF (@regular_connections IS NOT NULL)
  255.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  256.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  257.                                             N'RegularConnections',
  258.                                             N'REG_DWORD',
  259.                                             @regular_connections
  260.  
  261.   DECLARE @OS int
  262.   EXECUTE master.dbo.xp_MSplatform @OS OUTPUT
  263.  
  264.   IF (@regular_connections = 0)
  265.   BEGIN
  266.     IF (@OS = 2)
  267.     BEGIN
  268.       EXECUTE master.dbo.xp_instance_regdeletevalue N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'HostLoginID'
  269.       EXECUTE master.dbo.xp_instance_regdeletevalue N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'HostPassword'
  270.     END
  271.     ELSE
  272.     BEGIN
  273.       EXECUTE master.dbo.xp_sqlagent_param    2, N'HostLoginID'
  274.       EXECUTE master.dbo.xp_sqlagent_param    2, N'HostPassword'
  275.     END
  276.   END
  277.  
  278.   IF (@host_login_name IS NOT NULL)
  279.   BEGIN
  280.     IF (@OS = 2)
  281.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  282.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  283.                                             N'HostLoginID',
  284.                                             N'REG_SZ',
  285.                                             @host_login_name
  286.     ELSE
  287.     EXECUTE master.dbo.xp_sqlagent_param    1,
  288.                                             N'HostLoginID',
  289.                                             @host_login_name
  290.   END
  291.  
  292.   IF (@host_login_password IS NOT NULL)
  293.   BEGIN
  294.     IF (@OS = 2)
  295.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  296.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  297.                                             N'HostPassword',
  298.                                             N'REG_BINARY',
  299.                                             @host_login_password
  300.     ELSE
  301.     EXECUTE master.dbo.xp_sqlagent_param    1,
  302.                                             N'HostPassword',
  303.                                             @host_login_password
  304.   END
  305.  
  306.   IF (@login_timeout IS NOT NULL)
  307.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  308.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  309.                                             N'LoginTimeout',
  310.                                             N'REG_DWORD',
  311.                                             @login_timeout
  312.   IF (@idle_cpu_percent IS NOT NULL)
  313.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  314.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  315.                                             N'IdleCPUPercent',
  316.                                             N'REG_DWORD',
  317.                                             @idle_cpu_percent
  318.   IF (@idle_cpu_duration IS NOT NULL)
  319.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  320.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  321.                                             N'IdleCPUDuration',
  322.                                             N'REG_DWORD',
  323.                                             @idle_cpu_duration
  324.   IF (@oem_errorlog IS NOT NULL)
  325.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  326.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  327.                                             N'OemErrorLog',
  328.                                             N'REG_DWORD',
  329.                                             @oem_errorlog
  330.   IF (@sysadmin_only IS NOT NULL)
  331.     BEGIN
  332.     IF (@sysadmin_only = 1)
  333.       BEGIN
  334.     EXECUTE master.dbo.xp_sqlagent_proxy_account N'DEL'
  335.       END
  336.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  337.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  338.                                             N'SysAdminOnly',
  339.                                             N'REG_DWORD',
  340.                                             @sysadmin_only
  341.     END
  342.  
  343.   IF (@email_profile IS NOT NULL)
  344.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  345.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  346.                                             N'EmailProfile',
  347.                                             N'REG_SZ',
  348.                                             @email_profile
  349.   IF (@email_save_in_sent_folder IS NOT NULL)
  350.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  351.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  352.                                             N'EmailSaveSent',
  353.                                             N'REG_DWORD',
  354.                                             @email_save_in_sent_folder
  355.   IF (@cpu_poller_enabled IS NOT NULL)
  356.   BEGIN
  357.     EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  358.                                            N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  359.                                            N'CoreEngineMask',
  360.                                            @existing_core_engine_mask OUTPUT,
  361.                                            N'no_output'
  362.     IF ((@existing_core_engine_mask IS NOT NULL) OR (@cpu_poller_enabled = 1))
  363.     BEGIN
  364.       IF (@cpu_poller_enabled = 1)
  365.         SELECT @cpu_poller_enabled = (ISNULL(@existing_core_engine_mask, 0) & ~32)
  366.       ELSE
  367.         SELECT @cpu_poller_enabled = (ISNULL(@existing_core_engine_mask, 0) | 32)
  368.  
  369.       IF ((@existing_core_engine_mask IS NOT NULL) AND (@cpu_poller_enabled = 32))
  370.         EXECUTE master.dbo.xp_instance_regdeletevalue N'HKEY_LOCAL_MACHINE',
  371.                                                       N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  372.                                                       N'CoreEngineMask'
  373.       ELSE
  374.         EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  375.                                                 N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  376.                                                 N'CoreEngineMask',
  377.                                                 N'REG_DWORD',
  378.                                                 @cpu_poller_enabled
  379.     END
  380.   END
  381.  
  382.   RETURN(0) -- Success
  383. END
  384.  
  385. GO
  386. SET QUOTED_IDENTIFIER OFF 
  387. GO
  388. SET ANSI_NULLS ON 
  389. GO
  390.